﻿using Newtonsoft.Json;
using StrokeCenterSystem.Bll;
using StrokeCenterSystem.Core;
using StrokeCenterSystem.ThreeCenterManage;
using StrokeCenterSystem.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StrokeCenterSystem.Areas.StrokeCenter.Controllers
{
    public class ReportStatusController : BaseController
    {
        //
        // GET: /StrokeCenter/ReportStatus/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 患者总览加载
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            string name = WebUtils.GetString("name");
            int status = WebUtils.GetInt("status",-1);
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", 50);
            OperateResult resultInfo = PatientManager.GetPatientOverviewReport(pageIndex, pageSize, name, -1, -1, -1, "", status);
            string NoData = "{\"Total\": 0,\"Rows\": [], \"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            if (resultInfo.Result == 1)
            {
                try
                {
                    var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                    var result = new { Total = listData.PatientNum.Value, Rows = listData.PatientList, InHospitalNum = listData.InHospitalNum.Value, PreHospitalNum = listData.PreHospitalNum.Value, PathWayNum = listData.PathWayNum.Value };
                    return JsonDate(result);
                }
                catch
                { 
                }
            }
            else
            {
                NoData = "{\"Total\": 0,\"Rows\":[],\"Result\":\"" + resultInfo.Result + "\",\"Body\":\"" + resultInfo.Body + "\",\"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            }
            return Content(NoData);
        }

        /// <summary>
        /// 一键上报
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetPatientApprovalStatus(string patientId)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfBoolean result = service.SetPatientApprovalStatus(patientId, "", 1, loginUser.Name);
                if (!result.S)
                {
                    resultInfo.Body = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 上报状态的相关信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Message()
        {
            Reviewers_Information reviewInfo = new Reviewers_Information();
            string patientId = WebUtils.GetString("pid");
            if (patientId != "")
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfReviewers_Information result = service.GetReviewers_Information_PatientID(patientId);
                if (result.S)
                {
                    reviewInfo = result.D;
                }
            }
            ViewBag.ReviewInfo = reviewInfo;
            return View();
        }
    }
}
